package com.rest.demo.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author Administrator
 * @decription 数据从一个channel传输到另一个channel
 * @Date 2018-02-07 15:31
 */
public class ChannelFromDemo {

    public static void main(String[] args) throws IOException {
        RandomAccessFile fromFile = new RandomAccessFile("F:\\a.txt","rw");
        FileChannel fromChannel = fromFile.getChannel();

        RandomAccessFile toFile = new RandomAccessFile("F:\\b.txt","rw");
        FileChannel toChannel = toFile.getChannel();

        int position = 0;
        long count = fromChannel.size();
        //将别的channel的数据传输到自己的channel中
        toChannel.transferFrom(fromChannel,position,count);
        //将自己的channel数据传输到别人的channel中
        fromChannel.transferTo(position,count,toChannel);
    }
}
